const jsonpath = require("jsonpath");
 function replace(json, modify) {
  Object.keys(modify).forEach((key) => {
    jsonpath.apply(json, key, (v) => {
      let modifyValue = modify[key];
      let type = "string";
      if (typeof modifyValue === "object") {
        type = modifyValue.type;
        modifyValue = modifyValue.value;
      }
      
      if (typeof modifyValue === "number"||typeof modifyValue=='boolean') {
        return modifyValue;
      }
      const replaceResult = modifyValue.replace(/{(.*?)}/g, (match, p1) => {
        let value = jsonpath.query(json, p1, 1)[0];
        if (!value) {
          return `{${p1}}`;
        }
        return typeof value === "object" ? JSON.stringify(value) : value;
      });
      if (type === "object") {
        return JSON.parse(replaceResult);
      } else if (type === "number") {
        return Number(replaceResult);
      }
      return replaceResult;
    });
  });
  return json;
}
Object.defineProperty(module, 'exports', {
  enumerable: false,
  get: () => ({replace})
});